package Mypackage.Runner;

/**
 * Created by IntelliJ IDEA.
 * User: nadya
 * Date: Apr 7, 2011
 * Time: 11:33:56 PM
 * To change this template use File | Settings | File Templates.
 */

import Mypackage.PerlFileType;
import com.intellij.execution.Location;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
import com.intellij.execution.junit.RuntimeConfigurationProducer;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
//import com.sylvanaar.idea.Lua.sdk.KahluaSdk;
//import com.sylvanaar.idea.Lua.sdk.LuaSdkType;

/**
 * This class is based on code of the intellij-batch plugin.
 *
 * @author wibotwi, jansorg, sylvanaar
 */
public class PerlRunConfigurationProducer extends RuntimeConfigurationProducer implements Cloneable {
    private PsiFile sourceFile = null;

    public PerlRunConfigurationProducer() {
        super(PerlConfigurationType.getInstance());
    }

    @Override
    public PsiElement getSourceElement() {
        return sourceFile;
    }

    @Override
    protected RunnerAndConfigurationSettingsImpl createConfigurationByElement(Location location, ConfigurationContext configurationContext) {
        sourceFile = location.getPsiElement().getContainingFile();

        if (sourceFile != null && sourceFile.getFileType().equals(PerlFileType.PERL_FILE_TYPE)) {
            Project project = sourceFile.getProject();
            RunnerAndConfigurationSettings settings = cloneTemplateConfiguration(project, configurationContext);

            VirtualFile file = sourceFile.getVirtualFile();

            PerlRunConfiguration runConfiguration = (PerlRunConfiguration) settings.getConfiguration();
            if (file != null) {
                runConfiguration.setName(file.getName());

                runConfiguration.setScriptName(file.getPath());
                if (file.getParent() != null) {
                    runConfiguration.setWorkingDirectory(file.getParent().getPath());
                }
            }

            Module module = ModuleUtil.findModuleForPsiElement(location.getPsiElement());
            if (module != null) {
                runConfiguration.setModule(module);
            }

            return (RunnerAndConfigurationSettingsImpl) settings;
        }

        return null;
    }

    public int compareTo(Object o) {
        return 0;
    }
}
